package vn.com.aptech.msh.controller;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import vn.com.aptech.msh.business.IUserBean;
import vn.com.aptech.msh.entities.User;
import vn.com.aptech.msh.utils.Credentials;
import vn.com.aptech.msh.utils.NavigationHelper;
import vn.com.aptech.msh.utils.NotAuthorizedException;
import vn.com.aptech.msh.utils.ResourceBundleHelper;

/**
 *
 * @author trietto
 */
@Named("loginController")
@SessionScoped
public class LoginController implements Serializable {

    public static int ROLE_ADMIN = 1;
    public static int ROLE_MOD = 2;
    public static int ROLE_BUYER = 3;
    public static int ROLE_SELLER = 4;

    @Inject
    Credentials credentials;
    @Inject
    IUserBean userBusiness;
    private User user = null;

    public String login() {
        FacesMessage msg = null;
        user = userBusiness.authenticate(credentials.getUsername(), credentials.getPassword());
        if (user == null) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("login.failed.title"), ResourceBundleHelper.get("login.failed.detail"));
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } else {
            return NavigationHelper.toSuccessPage("login");
        }
    }

    public String logout() {
        user = null;
        return NavigationHelper.toSuccessPage("logout");
    }

    public void authorize(int role) throws NotAuthorizedException {
        boolean flag = true;
        if (isLoggedIn()) {
            if (user.getRole().getRoleID() == 1) {
                flag = false;
            }
            if (user.getRole().getRoleID() == role) {
                flag = false;
            }
        }
        if (flag) {
            throw new NotAuthorizedException();
        }
    }

    @Produces
    @Named("loggedIn")
    public boolean isLoggedIn() {
        return user != null;
    }

    public boolean isAdmin() {
        if (!isLoggedIn()) {
            return false;
        } else {
            return user.getRole().getRoleID() == ROLE_ADMIN;
        }
    }

    public boolean isMod() {
        if (!isLoggedIn()) {
            return false;
        } else {
            return user.getRole().getRoleID() == ROLE_MOD || isAdmin();
        }
    }

    public boolean isBuyer() {
        if (!isLoggedIn()) {
            return false;
        } else {
            return user.getRole().getRoleID() == ROLE_BUYER || isAdmin();
        }
    }

    public boolean isSeller() {
        if (!isLoggedIn()) {
            return false;
        } else {
            return user.getRole().getRoleID() == ROLE_SELLER || isAdmin();
        }
    }

    @Produces
    @Named("currentUser")
    User getCurrentUser() {
        return user;
    }

    public void setCurrentUser(User user) {
        this.user = user;
    }
}
